﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity = Savico.Library.Entity;
using Business = Savico.Library.Business;
using Common = Savico.Library.Commom.Algorithm;
using System.IO;

public partial class templates_admin_usercontrols_list_detailofbusiness : System.Web.UI.UserControl
{  
        Entity.LinhVucKinhDoanh entityLinhVucKinhDoanh = new Entity.LinhVucKinhDoanh();
        Business.LinhVucKinhDoanh businessLinhVucKinhDoanh = new Business.LinhVucKinhDoanh();
        Entity.DanhMucLinhVuc entityDanhMucLinhVuc = new Entity.DanhMucLinhVuc();
        Business.DanhMucLinhVuc businessDanhMucLinhVuc = new Business.DanhMucLinhVuc();
        public string ngonngu = "vi";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadTheoNgonNgu();
                LoadDanhMuc();
                
            }
        }
        #region Phân trang bằng PagedDataSource
        private static DataTable dtDanhMucLinhVuc = new DataTable();
        //Khái báo các property trang, chỉ số đầu, chỉ số cuối
        private int CurrentPage
        {
            get
            {
                object objPage = ViewState["_CurrentPage"];

                int _CurrentPage = 0;
                if (objPage == null)
                {
                    _CurrentPage = 0;
                }
                else
                {
                    _CurrentPage = (int)objPage;
                }
                return _CurrentPage;
            }
            set { ViewState["_CurrentPage"] = value; }
        }
        private int fistIndex
        {
            get
            {

                int _FirstIndex = 0;
                if (ViewState["_FirstIndex"] == null)
                {
                    _FirstIndex = 0;
                }
                else
                {
                    _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
                }
                return _FirstIndex;
            }
            set { ViewState["_FirstIndex"] = value; }
        }
        private int lastIndex
        {
            get
            {
                int _LastIndex = 0;
                if (ViewState["_LastIndex"] == null)
                {
                    _LastIndex = 0;
                }
                else
                {
                    _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
                }
                return _LastIndex;
            }
            set { ViewState["_LastIndex"] = value; }
        }

        PagedDataSource _PageDataSource = new PagedDataSource();

        //Load dữ liệu và phân trang cho dữ liệu
        private void BindItemsList(DataTable dt)
        {
            DataTable dataTable = dt;
            _PageDataSource.DataSource = dataTable.DefaultView;
            _PageDataSource.AllowPaging = true;
            _PageDataSource.PageSize = 5;
            _PageDataSource.CurrentPageIndex = CurrentPage;
            ViewState["TotalPages"] = _PageDataSource.PageCount;

            this.lblPageInfo.Text = "Trang " + (CurrentPage + 1) + " của " + _PageDataSource.PageCount;
            this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
            this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
            this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
            this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

            this.rptDanhMucLinhVuc.DataSource = _PageDataSource;
            this.rptDanhMucLinhVuc.DataBind();
            this.doPaging();
        }
        //Phân trang
        private void doPaging()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");
            fistIndex = CurrentPage - 5;
            if (CurrentPage > 5)
            {
                lastIndex = CurrentPage + 5;
            }
            else
            {
                lastIndex = 10;
            }
            if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
            {
                lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
                fistIndex = lastIndex - 10;
            }
            if (fistIndex < 0)
            {
                fistIndex = 0;
            }
            for (int i = fistIndex; i < lastIndex; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }
            this.dlPaging.DataSource = dt;
            this.dlPaging.DataBind();
        }

        //Trang dầu tiên
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 0;
            BindItemsList(dtDanhMucLinhVuc);
        }
        //Trang sau trang đang hiển thị
        protected void lbtnPrevious_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            BindItemsList(dtDanhMucLinhVuc);
        }
        //Trang tiếp theo trang đang hiển thị
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            BindItemsList(dtDanhMucLinhVuc);
        }
        //Trang cuối
        protected void lbtnLast_Click(object sender, EventArgs e)
        {
            CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
            BindItemsList(dtDanhMucLinhVuc);
        }
        //Hàm sự kiện dlPaging
        protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("Paging"))
            {
                CurrentPage = Convert.ToInt32(e.CommandArgument.ToString());
                BindItemsList(dtDanhMucLinhVuc);
            }
        }
        protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
            {
                lnkbtnPage.Enabled = false;
                //lnkbtnPage.Style.Add("fone-size", "14px");
                //lnkbtnPage.Font.Bold = true;
                lnkbtnPage.CssClass = "active";
            }
        }
        #endregion
        //Trung - Load và kiểm tra phân trang cho rpt
        private void LoadDanhMuc()
        {
            if (dtDanhMucLinhVuc.Rows.Count == 0)
            {
                BindItemsList(dtDanhMucLinhVuc);
                PhanTrang.Visible = false;
                //msgNoRows.Style.Add("display", "block");
            }
            else if (dtDanhMucLinhVuc.Rows.Count > 0 && dtDanhMucLinhVuc.Rows.Count <= 5)
            {
                //msgNoRows.Style.Add("display", "none");
                BindItemsList(dtDanhMucLinhVuc);
                PhanTrang.Visible = false;
            }
            else if (dtDanhMucLinhVuc.Rows.Count > 5)
            {
                //msgNoRows.Style.Add("display", "none");
                BindItemsList(dtDanhMucLinhVuc);
                PhanTrang.Visible = true;
            }
        }
        //Trung - Load Danh mục theo ngôn ngữ
        private void LoadTheoNgonNgu()
        {
            entityDanhMucLinhVuc.NgonNgu = ngonngu;
            dtDanhMucLinhVuc = businessDanhMucLinhVuc.SelectTheoNgonNgu(entityDanhMucLinhVuc);
 
        }
        //Trung - Event click control form
        protected void btnTiengViet_Click(object sender, EventArgs e)
        {
            ngonngu = "vi";
            LoadTheoNgonNgu();            
            LoadDanhMuc();
            ChuyenNgonNgu();
        }

        protected void btnTiengAnh_Click(object sender, EventArgs e)
        {
            ngonngu = "en";
            LoadTheoNgonNgu();            
            LoadDanhMuc();
            ChuyenNgonNgu();       
            
        }
        //Trung - Hàm chuyển đổi ngôn ngữ cho các control.text
        private void ChuyenNgonNgu()
        {
            foreach (RepeaterItem listItem in rptDanhMucLinhVuc.Items)
            {
                Label lbLinhVucEN = (Label)listItem.FindControl("lbLinhVuc");
                Label lbDanhMucEN = (Label)listItem.FindControl("lbDanhMuc");
                Label lbTomTat = (Label)listItem.FindControl("lbTomTat");
                HyperLink hplSuaEN = (HyperLink)listItem.FindControl("hplSua");
                LinkButton lbtXoaEN = (LinkButton)listItem.FindControl("lbtXoa");
                LinkButton lbtTrangThaiEN = (LinkButton)listItem.FindControl("lbtTrangThai");
                if (ngonngu == "en")
                {
                    
                    lbTitle.Text = "Business Scope ";
                    hplSuaEN.Text = "Edit";
                    lbtXoaEN.Text = "Delete";
                    lbtTrangThaiEN.Text = "Status";
                    lbLinhVucEN.Text = "Business Scope : ";
                    lbDanhMucEN.Text = "List : ";
                    lbTomTat.Text = "Summary : ";
                    btnLamMoi.Text = "Refresh";
                    btnThem.Text = "Add New";
                    btnTim.Text = "Search";
                    btnXoa.Text = "Delete";
                }
                if (ngonngu == "vi")
                {                    
                    lbTitle.Text = "Lĩnh vực kinh doanh";
                    hplSuaEN.Text = "Sửa";
                    lbtXoaEN.Text = "Xóa";
                    lbtTrangThaiEN.Text = "Trạng thái";
                    lbLinhVucEN.Text = "Lĩnh vực kinh doanh : ";
                    lbDanhMucEN.Text = "Danh mục : ";
                    lbTomTat.Text = "Tóm tắt : ";
                    btnLamMoi.Text = "Làm mới";
                    btnThem.Text = "Thêm";
                    btnTim.Text = "Tìm";
                    btnXoa.Text = "Xóa";
                }
            }     
        }

    }



